package com.xy.wsl.api.response;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;

@RestControllerAdvice
public class ControllerExceptionAdvice {
    @ExceptionHandler({BindException.class})
    public ErrorResult MethodArgumentNotValidExceptionHandler(BindException e) {
        // 从异常对象中拿到ObjectError对象
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        return new ErrorResult(objectError.getDefaultMessage());
    }

    @ExceptionHandler({AuthorizeException.class})
    public Result MethodArgumentNotValidExceptionHandler(AuthorizeException e) {
        // 从异常对象中拿到ObjectError对象
//        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        Result result = Result.noauth(e.getMsg());
        return result;
    }
    @ExceptionHandler({Exception.class})
    public ErrorResult MethodArgumentNotValidExceptionHandler(Exception e) {
        // 从异常对象中拿到ObjectError对象
//        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);r
        return new ErrorResult(e.getLocalizedMessage());
    }
}
